package com.main.hotel.service.impl;
import com.main.hotel.entity.Room;
import com.main.hotel.repository.RoomRepository;
import com.main.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class RoomServiceImpl implements RoomService {
@Autowired
private RoomRepository roomRepository;
  @Override
  public int findRoomCount(String room_type) {
    return roomRepository.findRoomCount(room_type);
  }

  @Override
  public int book(String room_type,String order_id) {
    return roomRepository.book(room_type,order_id);
  }

  @Override
  public int findRoomId(String order_id) {
    return roomRepository.findRoomId(order_id);
  }

  @Override
  public int cancelBook(String order_id) {
    return roomRepository.cancelBook(order_id);
  }

  @Override
  public int addRoom(Room room) {
    roomRepository.save(room);
    return 1;
  }

  @Override
  public Page<Room> findAll(int page, int pageSize) {
    Pageable pageable= PageRequest.of(page,pageSize);
    return roomRepository.findAll(pageable);
  }

  @Override
  public int updateRoomStatusByRoom_id(Integer room_id,Integer room_status) {
    Optional<Room> result = roomRepository.findById(room_id);
    Room room = result.get();
    room.setRoom_status(room_status);
    roomRepository.save(room);
    return 1;
  }
  @Override
  public int countRoomNum(Integer roomtype_id) {
    int roomNum;
    roomNum = roomRepository.findRoomNumByRoomtype_id(roomtype_id);
    return roomNum;
  }

  @Override
  public String findRoomNum(String order_id) {
    return roomRepository.findRoomNum(order_id);
  }
  @Override
  public int updateRemarkAndOrder_idByRoom_number(String room_number, String remark, String order_id) {
    Room room = roomRepository.findByRoom_number(room_number);
    room.setRemark(remark);
    room.setRoom_status(3);
    room.setOrder_id(order_id);
    roomRepository.save(room);
    return 1;
  }

  @Override
  public Room findByRoom_number(String room_number) {
    return roomRepository.findByRoom_number(room_number);
  }
}
